package com.fc.codegen.controller;

import cn.hutool.core.io.IoUtil;
import com.fc.codegen.dto.GenModuleDto;
import com.fc.codegen.service.GeneratorService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 模式生成
 * @ClassName: GenModuleController
 * @Author: weisn
 * @Date: 2023/2/7 14:39
 * @Version: 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/module")
@Tag(name = "表单管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GenModuleController {

    private final GeneratorService generatorService;
    @SneakyThrows
    @PostMapping("/code")
    public void generatorCode(@RequestBody GenModuleDto genModule, HttpServletResponse response) {
        byte[] data = generatorService.generatorModuleCode(genModule);
        response.reset();
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                String.format("attachment; filename=%s.zip", genModule.getProName()));
        response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
        response.setContentType("application/octet-stream; charset=UTF-8");

        IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
    }
}
